package com.imedical.exception;

import com.imedical.util.HttpContextUtil;
import com.imedical.util.R;

import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * 全局异常捕获<br>
 *
 * @author zhongyj
 * @date 2019/7/1
 */
@Slf4j
@Order(value = Ordered.HIGHEST_PRECEDENCE)
@ControllerAdvice
@RestControllerAdvice
public class GlobalExceptionHandler {

    /**
     * BizException
     *
     * @param e BizException
     * @return ResponseDTO
     */
    @ExceptionHandler(value = BizException.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public R handleBizException(BizException e) {
        //获取所有参数的map集合
        HttpServletRequest request = HttpContextUtil.getRequest();
        Map<String, String[]> parameterMap = request.getParameterMap();
        String param = JSONUtil.toJsonStr(parameterMap);
        String uri = request.getRequestURI();
        log.error("\n=================================== {} ===============================\n" +
                        "【 参数: {}】\n" +
                        "【 URI: {}】\n" +
                        "【 ACTION: {}】\n" +
                        "=========================================================================="
                , "业务异常, 抛出异常的请求参数", param, uri, request.getMethod());
        log.error("业务错误", e);
        return R.failed(e.getMessage());
    }

    @ExceptionHandler(value = DataException.class)
    @ResponseStatus(HttpStatus.OK)
    public R handleDataException(DataException e) {
        log.error("DataException数据异常，错误信息: 【 {} 】", e.getMessage());
        //获取所有参数的map集合
        HttpServletRequest request = HttpContextUtil.getRequest();
        Map<String, String[]> parameterMap = request.getParameterMap();
        String param = JSONUtil.toJsonStr(parameterMap);
        String uri = request.getRequestURI();
        log.error("\n=================================== {} ===============================\n" +
                        "【 参数: {}】\n" +
                        "【 URI: {}】\n" +
                        "【 ACTION: {}】\n" +
                        "=========================================================================="
                , "抛出异常的请求参数", param, uri, request.getMethod());
        return R.failed(e.getCode(), e.getMessage());
    }

    /**
     * Exception
     *
     * @param e Exception
     * @return ResponseDTO
     */
    @ExceptionHandler(value = Exception.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public R handleException(Exception e) {
        //获取所有参数的map集合
        HttpServletRequest request = HttpContextUtil.getRequest();
        Map<String, String[]> parameterMap = request.getParameterMap();
        String param = JSONUtil.toJsonStr(parameterMap);
        String uri = request.getRequestURI();
        log.error("\n=================================== {} ===============================\n" +
                        "【 参数: {}】\n" +
                        "【 URI: {}】\n" +
                        "【 ACTION: {}】\n" +
                        "=========================================================================="
                , "系统内部异常，抛出异常的请求参数", param, uri, request.getMethod());
        log.error("异常信息: ", e);
        return R.failed(e.getMessage());
    }
}













